<?php
/**
 * Created by PhpStorm.
 * User: 吕金鹏
 * Date: 2021/5/14
 * Time: 11:45 上午
 */

namespace app\index\controller;
use think\Db;
use think\facade\Request;

class Odo extends Base
{
    public function initialize()
    {
        parent::initialize();
    }

    public function index(){
        $list = Db::name("procure")->where("if_del",0)->order("id desc")->paginate(10);
//        dump($list);die;
        $this->assign('list',$list);
        $this->assign('page',$list->render());
        return $this->fetch();
    }
    public function addorder(){
        if(Request::isPost()){
            $data = input("post.");
            $data['time'] = time();
            $data['if_del'] = 0;
            $data['status'] = 1;
            $data['admin_id'] = $this->admin['id'];
            $data['admin_name'] = $this->admin['rel_name'];
            $res = Db::name("procure")->insert($data);
            if($res){
                return json(['code'=>1,"msg"=>'添加成功']);
            }

            return json(['code'=>0,'msg'=>'网络错误']);
        }
        $sn = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
//        echo $sn;die;
        $this->assign('sn',$sn);
        return $this->fetch();
    }
    public function changegoods(){
        if(Request::isPost()){
            $data = input('post.');
            $ids = $data['gid'];
            $oid = $data['oid'];
            $num = $data['num'];
            $ware = $data['w_id'];
            $goods_ary = [];

            foreach($ids as $key=>$val){
                $goods_ary[$key]['gid']=$val;
                $goods_ary[$key]['num'] =$num[$key];
                $goods_ary[$key]['innum'] = $num[$key];
                if(isset( $ware[$key])){
                    $goods_ary[$key]['ware_id'] = $ware[$key];
                }else{
                    return json(['code'=>0,"msg"=>'请选择仓库']);
                }
                $goods_ary[$key]['status'] = 1;
                $goods_ary[$key]['oid'] = $oid;
                $goods_ary[$key]['time'] = time();
                $goods_ary[$key]['name'] = Db::name("goods")->where('id',$val)->find()['name'];
            }
            //将商品信息存入数据库
            Db::name("procure_goods")->where("oid",$oid)->delete();
            $res = Db::name("procure_goods")->insertAll($goods_ary);
            Db::name('procure')->where('id',$oid)->update(["status"=>2]);
            if($res){
                return json(['code'=>1,"msg"=>'更新成功']);
            }else{
                return json(['code'=>0,"msg"=>'更新失败']);
            }
        }
        $id = input('id');
        $order = Db::name('procure')->where('id',$id)->find();
//        $gid_ary = json_decode($order['goods'],true);

        $goods = DB::name("procure_goods")->where("oid",$id)->select();
        foreach ($goods as $key=>$val){
            $goods_info = Db::name("goods")->where("id",$val['gid'])->find();
//            $ware_info = Db::name("warehouse")->where("id",$val["ware_id"])->find();
            $goods[$key]['specs'] = $goods_info['specs'];
            $goods[$key]['unit'] = $goods_info['unit'];
            $goods[$key]['ware'] = $val['ware_id'];
        }
        //查询仓库分类
        $ware = Db::name("warehouse")->where('status',1)->where("if_del",0)->select();
        $this->assign('ware',$ware);
        $this->assign('order',$order);
        $this->assign('goods',$goods);
        return $this->fetch();
    }
    public function addgoods(){
        $id = input('id');
        $goods = Db::name('goods')->where('status',1)->paginate(15)->each(function($item){
            $item['stock'] = Db::name("ware_goods")->where("gid",$item['id'])->sum('num');
            return $item;
        });
//        dump($goods);die;
        $this->assign('goods',$goods);
        $this->assign('page',$goods->render());
        $this->assign('id',$id);
        return $this->fetch();
    }
    public function adCar(){
        $data = input("post.");
        //存储数据
        $ids = $data['id'];
        $oid = $data['oid'];
        $goods_ary = [];

        foreach($ids as $key=>$val){
            $goods_ary[$key]['gid']=$val;
            $goods_ary[$key]['num'] =1;

            $goods_ary[$key]['status'] = 1;
            $goods_ary[$key]['oid'] = $oid;
            $goods_ary[$key]['time'] = time();
            $goods_ary[$key]['name'] = Db::name("goods")->where('id',$val)->find()['name'];
        }
        $res = Db::name("procure_goods")->insertAll($goods_ary);
        return json(['code'=>1,"msg"=>'添加成功']);

    }
    public function delgoods(){
        $id = input('id');
        $oid = input('oid');

        $order = Db::name('procure')->where('id',$oid)->find();
        $gid_ary = json_decode($order['goods'],true);

        if($gid_ary){
            foreach($gid_ary as $key=>$value){
                if($value['gid']==$id){
                    unset($gid_ary[$key]);
                }
            }
        }
        $goods = json_encode($gid_ary);
        Db::name("procure")->where('id',$oid)->update(['goods'=>$goods]);
        return json(['code'=>1,'msg'=>'删除成功']);
    }
    public function changeOrder(){
        $id = input('id');
        $status = input('status');
        if($status==3){
            $res  = Db::name("procure")->where('id',$id)->update(['status'=>$status,'send_time'=>time()]);
        }else{
            Db::name("procure")->where('id',$id)->update(['status'=>$status,'finish_time'=>time()]);
        }

        return json(['code'=>1,"msg"=>'更新成功']);
    }
    public function Sureorder(){

        $id = input('id');
        $order = Db::name("procure")->where("id",$id)->find();
        $gid_ary = json_decode($order['goods'],true);
//        dump($gid_ary);die;
        if($gid_ary){
//            echo 123;die;
            foreach($gid_ary as $key=>$value){
                $goods[$key] = Db::name("goods")->where('id',$value['gid'])->where('status',1)->find();
                $goods[$key]['num'] = $value['num'];
                if(isset($value['fact'])){
                    $goods[$key]['fact'] = $value['fact'];
                }

                if(isset($value['ware'])){
                    $goods[$key]['ware'] = DB::name("warehouse")->where('id',$value['ware'])->find()['name'];
                    $goods[$key]["ware_id"] = $value['ware'];

                }
            }
        }
//        dump($goods);die;
        $this->assign("order",$order);
        $this->assign('goods',$goods);
        return $this->fetch();
    }
    public function Suregoods(){
        $id = input('gid');
        $num = input("num");
        $oid = input("oid");
        $wid = input("wid");
        $data_goods['name'] = Db::name("goods")->where('id',$id)->field("name")->find()['name'];
        $data_goods['num'] = $num;
        $data_goods['add_time'] = time();
        $data_goods['gid'] = $id;
        $data_goods['wid'] = $wid;
        //查看仓库中师傅存在相同货品
        $res = Db::name("ware_goods")->where("gid",$id)->where("wid",$wid)->find();
        //入库数量
        if(!$res){
            Db::name("ware_goods")->insert($data_goods);
        }else{
            //当存在相同商品的时候
            Db::name("ware_goods")->where("gid",$id)->where("wid",$wid)->setInc('num',$num);
            Db::name("ware_goods")->where("gid",$id)->where("wid",$wid)->update(['last_time'=>time()]);
        }
        //修改入库单
        $order = Db::name('procure')->where('id',$oid)->find();
        $gid_ary = json_decode($order['goods'],true);
        $goods_ary = [];
        $sure = 1;
        $finish = 0;
        foreach($gid_ary as $key=>$val){
            $goods_ary[$key]['gid']=$val['gid'];
            $goods_ary[$key]['num'] =$val['num'];
            $goods_ary[$key]["ware"] = $val['ware'];
            if($val['gid']==$id){
                $goods_ary[$key]['fact'] = $num;
            }
            if(isset($val['fact'])){
                $goods_ary[$key]['fact'] = $val['fact'];
            }
            if(!isset($goods_ary[$key]['fact'])) $sure = 0;
        }
        if(!$sure){
            Db::name('procure')->where('id',$oid)->update(['goods'=>json_encode($goods_ary)]);
        }else{
            Db::name('procure')->where('id',$oid)->update(['goods'=>json_encode($goods_ary),'status'=>4,'finish_time'=>time()]);
            $finish = 1;
        }

        //存入log
        $log_data['time'] = time();
        $log_data['type'] = 1;
        $log_data['gid'] = $id;
        $log_data['ware'] = $wid;
        $log_data['oid'] = $oid;
        $log_data['num'] = $num;
        if($finish){
            if(Db::name("ware_log")->insert($log_data)) return json(['code'=>2,'msg'=>"修改成功"]);
        }
        if(Db::name("ware_log")->insert($log_data)) return json(['code'=>1,'msg'=>"修改成功"]);

    }
    public function order(){
        $id = input("id");
        $order = Db::name('procure')->where('id',$id)->find();
        $gid_ary = json_decode($order['goods'],true);
        $goods_ary = [];

        foreach($gid_ary as $key=>$val){
            $goods_ary[$key] =Db::name('goods')->where('id',$val['gid'])->find();
            $goods_ary[$key]['num'] =$val['num'];
            $goods_ary[$key]["ware"] = Db::name('warehouse')->where('id',$val['ware'])->field("name")->find()['name'];


        }
        $this->assign("order",$order);
        $this->assign('goods',$goods_ary);
        $this->assign("title",'门店自采入库单');
        return $this->fetch();
    }
    public function delodo(){
        $id = input('id');
        $res = Db::name("procure")->where("id",$id)->delete();
        if($res){
            return json(['code'=>1,'msg'=>'删除成功']);
        }
        return json(['msg'=>0,'msg'=>'网络延时,请稍后再试']);
    }
    //查看看采购单入库情况
    public function check(){
        $id = input("id");
        //查询改采购单,入库商品
        $p_goods = Db::name("procure_goods")->where("oid",$id)->select();
        //查看商品入库是否确认
        foreach($p_goods as $key=>$val){
            $in_goods = Db::name("inorder_goods")->where("pg_id",$val['id'])->select();
            $innum = 0;
            //确认入库商品是否确认
            foreach($in_goods as $k=>$v){

                $res = Db::name("inorder")->where("id",$v['inorder_id'])->where("status",2)->find();

                if($res){
//                    dump($v);
                    $innum += $v['num'];
                }
            }
            $goods_info = Db::name("goods")->where("id",$val['gid'])->find();
//            $ware_info = Db::name("warehouse")->where("id",$val["ware_id"])->find();
            $p_goods[$key]['specs'] = $goods_info['specs'];
            $p_goods[$key]['unit'] = $goods_info['unit'];
            $p_goods[$key]['ware_name'] = Db::name("warehouse")->where('id',$val['ware_id'])->find()['name'];
            $p_goods[$key]['innum'] = $innum ;
        }
//        dump($p_goods);die;
        $order = Db::name("procure")->where("id",$id)->find();
        $this->assign("order",$order);
        $this->assign("goods",$p_goods);
        return $this->fetch();
    }
}
